package handlers

import (
	"github.com/gin-gonic/gin"
	"tvmanager/models"
)

func Add(c *gin.Context) {
	title := c.PostForm("title")
	content := c.PostForm("content")
	times := c.PostForm("times")
	if title == "" || content == "" || times == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不可为空",
		})
		return
	}
	data := models.Video{
		Title:   title,
		Content: content,
		Times:   times,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func List(c *gin.Context) {
	var list []models.Video
	models.DB.Find(&list)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": list,
	})
	return
	//page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	//
	//size, _ := strconv.Atoi(c.DefaultQuery("size", "5"))
	//Offset := (page - 1) * size
	//query := models.DB.Model(&models.Video{})
	//var count int64
	//query.Count(&count)
	//var list []models.Video
	//err := models.DB.Offset(Offset).Limit(size).Find(&list).Error
	//if err != nil {
	//	c.JSON(200, gin.H{
	//		"code": 500,
	//		"msg":  "查询失败",
	//	})
	//	return
	//}
	//c.JSON(200, gin.H{
	//	"code": 200,
	//	"msg":  "查询成功",
	//	"data": gin.H{
	//		"count": count,
	//		"list":  list,
	//	},
	//})
	//return
}
